<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/10/12
 * Time: 10:13
 */

namespace App\admin\TableView;


use App\admin\TableView\BaseClass\BaseViewList;
use App\Models\Admin\ApilistModel;

class ApilistView extends BaseViewList
{

    public function get_model()
    {

        return new ApilistModel();
    }

    public $topTitle = '接口';
    public $PageSize = 10;

    public $breadcrumb = '<li><a href="/mcadmin">Home</a></li>
        <li><a href="javascript:;">接口</a></li>
        ';

    public $html = '';


    //  public $show_add = false; ///显示添加按钮

    /**
     * 获取表格的唯一名字
     */
    public function getName()
    {
        return 'ApilistModel';
    }


    /**
     * 需要继承,然后定义每行需要显示的数据格式
     */
    public function Labels()
    {
        return array(
            //   BaseViewList::Button_checkbox=>[],
            'id' => [
                'width' => 40
            ],
            'name' => ['width' => 80],
            'api' => ['width' => 120],
            'call' => ['width' => 200],
            'auth' => ['width' => 70],
            'count' => ['width' => 50,
                'sort' => 'true'],
            'success' => ['width' => 40,
                'sort' => 'true'],
            'fail' => ['width' => 40,
                'sort' => 'true'],
            'notes' => ['width' => 180],
            'average_time' => ['width' => 90, 'title' => '平均耗时',
                'sort' => 'true'],
            'runtest' => ['width' => 100, 'title' => '测试状态'],
            'isrun' => ['width' => 60],
            BaseViewList::Button_control => [

                BaseViewList::Button_Edit,
                BaseViewList::Button_Delete

            ]
        );
    }

    /**
     * 对话框中的各个字段
     */
    public function dialog()
    {
        return array(
            array(
                'zd' => 'name'
            ),
             array(
                'zd' => 'api'
            ),
            array(
                'zd' => 'call'
            ),
            array(
                'zd' => 'auth',
                'type' => self::dialog_list,
                'data' => ApilistModel::getAuthArray()
            ),
            array(
                'zd' => 'isrun',
                'type' => self::dialog_list,
                'data' => [
                    1 => '启用',
                    0 => '关闭'
                ]
            ),
            array(
                'zd' => 'notes'
            ),
        );
    }

    /**
     * 方便继承,多一层过滤
     */
    public function ModelTohtml($key, $model)
    {
        if ($key == 'runtest') {
            return ApilistModel::getTest($model);
        }
        if ($key == 'isrun') {
            return $this->ArrayToTitle($model->$key, [
                1 => '启用',
                0 => '关闭'
            ]);
        }
        if ($key == 'auth') {
            return $this->ArrayToTitle($model->$key, ApilistModel::getAuthArray());
        }
        if ($key == 'average_time') {
            if ($model->count == 0 || empty($model->count)) {
                return '无效';
            }
            return round($model->runtime / $model->count / 1000, 3);
        }

        return parent::ModelTohtml($key, $model);
    }

}